package cn.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.dao.TeacherDao;
import cn.dao.ZhuanyeDao;
import cn.entity.TCompany;
import cn.entity.TStudent;
import cn.entity.TTeacher;
import cn.entity.TZhuanye;
import cn.util.PageBean;
@Controller("teacherAction")
@Scope("prototype")
public class TeacherAction extends BaseAction{
	private Integer id;
	private Integer zid;
	private TTeacher teacher=new TTeacher();
	private List<TTeacher> list;
	private String name;
	@Autowired
	@Qualifier("teacherDao")
	private TeacherDao teacherDao;
	@Autowired
	@Qualifier("zhuanyeDao")
	private ZhuanyeDao zhuanyeDao;
	
	private PageBean pageBean=new PageBean();
	
	public String list(){
		String sqlCount="select count(*) from TTeacher u where 1=1";
		String sql="from TTeacher u where 1=1";
		if(name!=null && !"".equals(name)){
			sqlCount+=" and u.name like '%"+name+"%'";
			sql+=" and u.name like '%"+name+"%'";	
		}
		sqlCount+=" and u.isdel=0";
		sql+=" and u.isdel=0";
		pageBean.setTotalCount(teacherDao.getCount(sqlCount));
		list=teacherDao.getByPage(sql, pageBean.getPageNo(), pageBean.getPageSize());
		return SUCCESS;
	}
	
	public String add(){
		teacher.setIsdel(0);
		teacherDao.add(teacher);
		return "list";
	}
	
	public String detail() throws IOException{
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out=response.getWriter();
		TTeacher tea=teacherDao.get(TTeacher.class, id);		
		out.print(JSON.toJSONString(tea));
		out.close();
		return null;
	}
	
	public String update(){
		Integer role=(Integer)session.getAttribute("role");
		TTeacher tea=teacherDao.get(TTeacher.class, id);
		TZhuanye z=zhuanyeDao.get(TZhuanye.class, zid);
		tea.setZhuanye(z);
		if(teacher.getEmail()!=null && !"".equals(teacher.getEmail())){
			tea.setEmail(teacher.getEmail());
		}
		if(teacher.getName()!=null && !"".equals(teacher.getName())){
			tea.setName(teacher.getName());
		}
		if(teacher.getTel()!=null && !"".equals(teacher.getTel())){
			tea.setTel(teacher.getTel());
		}		
		teacherDao.update(tea);
		if(role==3){
			try {
				response.sendRedirect("teacher_toupdate?id="+tea.getId());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}else{
			return "list";
		}
	}
	
	public String toupdate(){
		teacher=teacherDao.get(TTeacher.class, id);
		return "detail";
	}
	
	public String delete(){
		TTeacher s=teacherDao.get(TTeacher.class, id);
		s.setIsdel(1);
		teacherDao.update(s);
		return "list";
	}
	
	public String listJson() throws IOException{
		List<TTeacher> teacherList=teacherDao.getByHql("from TTeacher where isdel=0");
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out=response.getWriter();
		JSONArray teachers=new JSONArray();
		for(TTeacher t : teacherList){
			JSONObject teacher=new JSONObject();
			teacher.put("id", t.getId());
			teacher.put("name",t.getName());
			teachers.add(teacher);
		}
		out.print(teachers.toJSONString());
		out.close();
		return null;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public TTeacher getTeacher() {
		return teacher;
	}

	public void setTeacher(TTeacher teacher) {
		this.teacher = teacher;
	}

	public List<TTeacher> getList() {
		return list;
	}

	public void setList(List<TTeacher> list) {
		this.list = list;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public TeacherDao getTeacherDao() {
		return teacherDao;
	}

	public void setTeacherDao(TeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public Integer getZid() {
		return zid;
	}

	public void setZid(Integer zid) {
		this.zid = zid;
	}

	public ZhuanyeDao getZhuanyeDao() {
		return zhuanyeDao;
	}

	public void setZhuanyeDao(ZhuanyeDao zhuanyeDao) {
		this.zhuanyeDao = zhuanyeDao;
	}

}
